import axios from '@/utils/request'
let store = 0
export default {
  StoreRole: {
    entity: { name: 'Role', prefix: () => {
      store = localStorage.getItem('storeId')
      return `store/${store}`
    } },
    list: {
      list_filter: {},
      list_display: [
        'name',
        {
          property: 'categories',
          label: '可见产品分类',
          component: {
            props: ['data'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              axios.get(`/store/${store}/categories`, { params: { '@filter': `entity.getId() in [${this.data.toString()}]` }}).then((res) => {
                this.categories = res.data
                console.log('categories', res.data)
              })
            },
            render(h) {
              return (
                <div >
                  {
                    this.categories.map(v => {
                      return <div>{v.name}</div>
                    })
                  }
                </div>
              )
            }
          }
        },
        {
          property: 'defaultDailySalary',
          label: '默认日薪'
        },
        {
          property: 'defaultBond',
          label: '默认保证金'
        },
        {
          property: 'defaultOnboardFee',
          label: '默认入职费用'
        },
        {
          property: 'disableAutoOrderSettle',
          label: '不自动分账'
        }
      ]
    },
    form: {
      fields: [
        'name',
        {
          property: 'categories',
          field_options: { label: '可见产品类型' },
          required: false,
          default_value: [],
          component: {
            props: ['form', 'property'],
            data() {
              return {
                categories: []
              }
            },
            created() {
              this.axios.get(`/store/${store}/categories`, { params: {'@filter': `entity.getType().getSlug() == 'product'`, 'limit': 1000000 }}).then((res) => {
                const categories = []
                for (var i of res.data) {
                  categories.push({
                    label: i.name,
                    key: i.id
                  })
                }
                this.categories = categories
              })
            },
            render(h) {
              return (
                <el-transfer v-model={this.form[this.property]} data={this.categories} titles={['未选', '已选']}></el-transfer>
              )
            }
          }
        },
        {
          property: 'defaultDailySalary',
          field_options: { label: '默认日薪' }
        },
        {
          property: 'defaultBond',
          field_options: { label: '默认保证金' }
        },
        {
          property: 'defaultOnboardFee',
          field_options: { label: '默认入职费用' }
        },
        // {
        //   property: 'defaultExtraData',
        //   default_value: {
        //     isPrincess: false,
        //     isBooker: false,
        //     isCashier: false,
        //     canCashier: false,
        //     noStatistics: false,
        //     changeRoule: false,
        //     edit: false,
        //     canPenaltie: false,
        //     noCancelOrder: false,
        //     noChangeSquad: false,
        //     noChangeRole: false,
        //     attendanceOrder: false
        //   },
        //   field_options: { label: '角色默认权限' },
        //   help: 'noCancelOrder:不能取消订单 ；isPrincess:房间公主 ,isBooker: 可以订台,isCashier: 查看所有结算订单,canCashier: 可以统收订单,noStatistics: 不查看总报表,changeRoule: 队长经纪可修改下属信息,edit: 管理员可修改员工信息;attendanceOrder:不打卡不能下单'
        // },
        {
          property: 'defaultExtraData',
          field_options: { label: '角色权限' },
          component: {
            props: ['form', 'property'],
            data() {
              return {
                item: {
                  isPrincess: false,
                  isBooker: false,
                  isCashier: false,
                  canCashier: false,
                  noStatistics: false,
                  changeRoule: false,
                  edit: false,
                  canPenaltie: false,
                  noCancelOrder: false,
                  noChangeSquad: false,
                  noChangeRole: false,
                  attendanceOrder: false,
                  noClock: false
                },
                label: {
                  isPrincess: '下单默认下首个产品(前公主权限)',
                  isBooker: '可以订台',
                  isCashier: '查看所有订单',
                  canCashier: '查看自己下单的订单',
                  noStatistics: '不查看统计报表(管理员权限)',
                  changeRoule: '作为队长/经纪人时可修改下属角色',
                  edit: '作为场所管理员时可修改员工角色',
                  canPenaltie: '可执行扣罚',
                  noCancelOrder: '不能取消订单',
                  noChangeSquad: '场所管理员有修改权限时候不能修改员工所属小队',
                  noChangeRole: '场所管理员有修改权限时候不能修改员工角色',
                  attendanceOrder: '不打卡不能下单',
                  noClock: '作为管理或者队长时不显示考勤码'
                }
              }
            },
            methods: {
              // 获取数据有config字段再初始化数据
              setConfig(value) {
                if (Object.keys(value).includes('defaultExtraData')) {
                  if (!value.defaultExtraData) {
                    return
                  }
                  if (value.defaultExtraData.isPrincess) {
                    this.item.isPrincess = value.defaultExtraData.isPrincess
                  }
                  if (value.defaultExtraData.isBooker) {
                    this.item.isBooker = value.defaultExtraData.isBooker
                  }
                  if (value.defaultExtraData.noClock) {
                    this.item.noClock = value.defaultExtraData.noClock
                  }
                  if (value.defaultExtraData.isCashier) {
                    this.item.isCashier = value.defaultExtraData.isCashier
                  }
                  if (value.defaultExtraData.canCashier) {
                    this.item.canCashier = value.defaultExtraData.canCashier
                  }
                  if (value.defaultExtraData.noStatistics) {
                    this.item.noStatistics = value.defaultExtraData.noStatistics
                  }
                  if (value.defaultExtraData.changeRoule) {
                    this.item.changeRoule = value.defaultExtraData.changeRoule
                  }
                  if (value.defaultExtraData.edit) {
                    this.item.edit = value.defaultExtraData.edit
                  }
                  if (value.defaultExtraData.noCancelOrder) {
                    this.item.noCancelOrder = value.defaultExtraData.noCancelOrder
                  }
                  if (value.defaultExtraData.noChangeSquad) {
                    this.item.noChangeSquad = value.defaultExtraData.noChangeSquad
                  }
                  if (value.defaultExtraData.noChangeRole) {
                    this.item.noChangeRole = value.defaultExtraData.noChangeRole
                  }
                  if (value.defaultExtraData.attendanceOrder) {
                    this.item.attendanceOrder = value.defaultExtraData.attendanceOrder
                  }
                  if (value.defaultExtraData.canPenaltie) {
                    this.item.canPenaltie = value.defaultExtraData.canPenaltie
                  }
                  if (value.defaultExtraData.depositCommission) {
                    this.item.depositCommission = value.defaultExtraData.depositCommission
                  }
                }
              },
              updateProperty() {
                this.form[this.property] = this.item
              }
            },
            watch: {
              // immediate监听form
              'form': {
                handler(value, oldValue) {
                  this.setConfig(value)
                },
                immediate: true
              }
            },
            render(h) {
              return (
                <div>
                  {
                    Object.keys(this.item).map(v => {
                      // 特殊的，非勾选的设置项
                      // const noDefaultConfig = ['transactionFeeRate', 'marketingExpenses', 'marketingPeople', 'withdrawEndTime', 'withdrawStartTime']
                      // if (noDefaultConfig.indexOf(v) === -1) {
                      return <div><el-checkbox label={this.label[v]} v-model={this.item[v]} onChange={() => this.updateProperty()} /></div>
                      // }
                    })
                  }
                </div >
              )
            }
          }
        },
        {
          property: 'disableAutoOrderSettle',
          field_options: { label: '不自动分账' },
          help: '只是针对门店已经设置了自动分账的情况，对应角色不会执行自动分账，门店管理员可以执行手动分账',
          default_value: false
        },
        {
          property: 'extraData',
          default_value: {
            offLock: false
          },
          field_options: { label: '分账触发' },
          help: 'offClock: 打下班卡后自动分账--角色设置了不自动分账的时候，才会触发这里的设置'
        }
      ]
    }
  }
}

